package com.bicon.botu.account.rpc.service;

import com.bicon.botu.account.dto.AccountInfoDTO;
import com.bicon.botu.account.entity.AccountInfo;
import com.bicon.botu.account.service.IAccountRoleService;
import com.bicon.botu.account.service.IAccountService;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import com.bicon.botu.core.base.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @auther liushujie
 * @date 2017/12/4 10:59
 */
@Service
public class AccountRpcService extends BaseController {

    @Autowired
    private IAccountService iAccountService;

    @Autowired
    private IAccountRoleService iAccountRoleService;

    /**
     * 后台添加app用户及角色
     *
     * @param account  账户
     * @param code     角色code
     * @param type     登录类型， 登录类型:1移动端，2后台管理端，3平台端
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "/addAccountAndRole")
    public BaseResult addAccountAndRole(@RequestParam String account, @RequestParam String code, @RequestParam Integer type, @RequestParam String password) {
        return iAccountService.addAccountAndRole(account, code, type, password);
    }

    /**
     * 添加账户角色
     *
     * @param accountId 账户id
     * @param code      角色code
     * @return
     */
    @RequestMapping(value = "/addAccountRole")
    public BaseResult addAccountRole(@RequestParam String accountId, @RequestParam String code) {
        return iAccountRoleService.addAccountRole(accountId, code);
    }

    /**
     * 删除账号角色关联
     *
     * @param accountId 账号id
     * @param type      账户类型：0：前台账户，1：后台账户
     * @param code      角色code
     * @return
     */
    @RequestMapping(value = "/delete")
    public BaseResult delete(@RequestParam String accountId, @RequestParam String type, @RequestParam String code) {
        BaseResult baseResult = iAccountRoleService.deleteAccountRole(accountId, type, code);
        return baseResult;
    }


    /**
     * 新增账户
     *
     * @param account  账号
     * @param password 密码
     * @param enable   类型：0：正常、1：禁用状态
     * @param type     类型(账号0、邮箱1、手机号2、第三方微信3、后台管理4)
     * @param phone    电话
     * @return
     */
    @RequestMapping(value = "/saveAccount", method = RequestMethod.POST)
    public BaseResult saveAccount(@RequestParam String account, @RequestParam String password, @RequestParam Integer enable, @RequestParam Integer type,  String phone) {
        if(iAccountService.saveAccount(account, password, enable, type)){
            return sendResult200();
        }
        return sendResult500();
    }

    /**
     * 获取账户名
     * @return
     */
    @RequestMapping(value = "/getAccount", method = RequestMethod.POST)
    public BaseResult getAccountLog(@RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return sendResult400();
        }
        AccountInfo account = new AccountInfo().selectById(userId);
        AccountInfoDTO accountInfoDTO = new AccountInfoDTO();
        BeanMapper.copy(account, accountInfoDTO);
        return sendResult200(accountInfoDTO);
    }


}
